home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK1.toast / Development Kits (Disc 1) / Open Transport / Sample Code / DLPI / ATM PCI DLPI / Sources / PCIRoutines.c < prev    next >
Encoding:
Text File  |  1995-05-17  |  8.4 KB  |  288 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PCIRoutines.c
  3.  
  4.     Contains:    This file contains the routine(s) that are related to the PCI bus.
  5.     
  6.     Written by:    
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.  
  13.     To Do:
  14.     
  15. */
  16.  
  17. //-----------------------------------------------------------------------------------------
  18. //    Header files
  19. //-----------------------------------------------------------------------------------------
  20.  
  21. #include <OpenTptModule.h>                // Open Transport files
  22. #include <OpenTptDevLinks.h>
  23. #include <Types.h>
  24.  
  25. #include <Kernel.h>                        // System files
  26. #include <DriverServices.h>
  27. #include <NameRegistry.h>
  28. #include <Interrupts.h>
  29. #include <PCI.h>
  30.  
  31. #include "PCIRoutines.h"                // our files
  32. #include "HWSpecific.h"
  33.  
  34. //-----------------------------------------------------------------------------------------
  35. // Global variable for the entire CFM
  36. //-----------------------------------------------------------------------------------------
  37.  
  38. extern PortPrivateData *gPortPrivateData;        // Declared in ATMDLPI.c             
  39.  
  40. //-----------------------------------------------------------------------------------------
  41. //    Description:
  42. //        This routine extracts the logical address by using the "assigned addresses"  
  43. //        property and the "AAPL,address" property.  Each card asks the System for certain
  44. //        address spaces depending on their needs.  The config space allows for the
  45. //        card to have upto 6 spaces plus a ROM space.
  46. //
  47. //        This routine accepts as input the offset into config space that matches their
  48. //        space request. In other words, if I am using an Ethernet card it may want two
  49. //        address spaces; I/O and Memory.  The card is designed so that offset 0x10 in
  50. //        config space corresponds to the I/O space and 0x14 corresponds to the Memory
  51. //        space.
  52. //
  53. //        The following values are valid for offsetValues (defined in PCIRoutines.h):
  54. //
  55. //            #define kPCIConfigBase10Offset                0x10
  56. //            #define kPCIConfigBase14Offset                0x14
  57. //            #define kPCIConfigBase18Offset                0x18
  58. //            #define kPCIConfigBase1COffset                0x1C
  59. //            #define kPCIConfigBase20Offset                0x20
  60. //            #define kPCIConfigBase24Offset                0x24
  61. //            #define kPCIConfigBaseROM30Offset            0x30
  62. //
  63. //    Input:
  64. //        theID - the NameRegistry ID for a PCI card
  65. //        baseRegAddress - no input value
  66. //        offsetValue - config base offset, determines which address space logically
  67. //            address is returned
  68. //        spaceAllocated - no input value
  69. //
  70. //    Output:
  71. //        if err = kOTNoError, *baseRegAddress - contains the logical address of a PCI 
  72. //            address space, also spaceAllocated is a byte count for the amount of space
  73. //            that was allocated
  74. //        returns various errors
  75. //
  76. //-----------------------------------------------------------------------------------------
  77. OSStatus GetPCICardBaseAddress(RegEntryID *theID, UInt32 *baseRegAddress, UInt8 offsetValue,
  78.     UInt32 *spaceAllocated)     
  79. {
  80. OSStatus                osStatus;
  81. PCIAssignedAddress        *assignedArray;    
  82. RegPropertyValueSize    propertySize;
  83. UInt32                    numberOfElements, *virtualArray;
  84. Boolean                    foundMatch;
  85. UInt16                    index;
  86.  
  87. *baseRegAddress = NULL;        // default value
  88. foundMatch = kFalse;
  89.  
  90. osStatus = GetAProperty(theID, kPCIAssignedAddressProperty,(void **)&assignedArray,
  91.     &propertySize);
  92.  
  93. if ((osStatus == kOTNoError) && propertySize)
  94.     {
  95.     numberOfElements = propertySize/sizeof(PCIAssignedAddress);
  96.     
  97.     osStatus = GetAProperty(theID, kAAPLDeviceLogicalAddress,(void **)&virtualArray,
  98.         &propertySize);
  99.     
  100.     if ((osStatus == kOTNoError) && propertySize)
  101.         {
  102.             // search through the assigned addresses property looking for base register
  103.         for (index = 0; (index != numberOfElements) && !foundMatch; ++index)
  104.             {
  105.             if (assignedArray[index].registerNumber == offsetValue)
  106.                 {
  107.                 *spaceAllocated = assignedArray[index].size.lo;
  108.                 *baseRegAddress = virtualArray[index];
  109.                 foundMatch = kTrue;
  110.                 }
  111.             }
  112.         DisposeProperty((void **)&virtualArray);
  113.         }
  114.     else
  115.         osStatus = kENXIOErr;
  116.  
  117.     DisposeProperty((void **)&assignedArray);
  118.     }
  119. else            
  120.     osStatus = kENXIOErr;
  121.         
  122. return osStatus;
  123. }
  124.  
  125. //-----------------------------------------------------------------------------------------
  126. //    Description:
  127. //        This function gets a copy of the property from the tree, allocates memory for it and 
  128. //        copies it in.  The memory allocation is only valid during non interrupt time!
  129. //
  130. //        THIS ROUTINE SHOULD ONLY BE CALLED AT NONINTERRUPT TIME
  131. //
  132. //    Input:
  133. //        theID - the NameRegistry of a device
  134. //        prop - an ascii string name of the property
  135. //        val - address to property value
  136. //        siz - address to size of property
  137. //
  138. //    Output:
  139. //        returns -1, if the property cannot be returned
  140. //
  141. //-----------------------------------------------------------------------------------------
  142. OSStatus GetAProperty(RegEntryID *theID,RegPropertyName *prop,RegPropertyValue *val,RegPropertyValueSize *siz)
  143. {
  144. OSStatus    err = kENXIOErr;
  145.  
  146. err = RegistryPropertyGetSize(theID,prop,siz);
  147. if (err == noErr)
  148.     {
  149.     *val = PoolAllocateResident(*siz,kTrue);
  150.     if (*val)        // did we get the memory that we asked for
  151.         {
  152.         if ((err = RegistryPropertyGet(theID,prop,*val,siz)) != NULL)
  153.             PoolDeallocate(*val);
  154.         }    
  155.     }
  156.  
  157. return err;
  158. }
  159.  
  160. //-----------------------------------------------------------------------------------------
  161. //    Description:
  162. //        This routine releases the memory allocated in GetAProperty.
  163. //
  164. //    Input:
  165. //        val - address of memory that needs to be deallocated
  166. //        
  167. //
  168. //    Output:
  169. //        NONE
  170. //
  171. //-----------------------------------------------------------------------------------------
  172. void DisposeProperty(RegPropertyValue *val)
  173. {
  174. if (*val != NULL)
  175.     PoolDeallocate(*val);
  176.  
  177. *val = NULL;
  178. }
  179.  
  180. //-----------------------------------------------------------------------------------------
  181. //    Description:
  182. //        This routine installs the interrupt service routine for our card.
  183. //
  184. //    Input:
  185. //        NONE
  186. //
  187. //    Output:
  188. //        returns error if isr is not installed correctly
  189. //
  190. //-----------------------------------------------------------------------------------------
  191. OSStatus InstallISR(void)
  192. {
  193. void                    *theRefcon;
  194. InterruptHandler        defaultISRFunction;        
  195. InterruptEnabler        defaultEnablerFunction;    
  196. InterruptDisabler        defaultDisablerFunction;
  197. OSStatus                osStatus;
  198. RegPropertyValueSize    propertySize;
  199. ISTProperty                theISTProperty;
  200.  
  201. propertySize = sizeof(ISTProperty);
  202.  
  203. osStatus = RegistryPropertyGet(&gPortPrivateData->nodeEntryID,
  204.                                 kISTPropertyName,
  205.                                 &theISTProperty,
  206.                                 &propertySize);
  207.                                 
  208. if (osStatus)
  209.     return osStatus;
  210.  
  211. osStatus = GetInterruptFunctions(theISTProperty[kISTChipInterruptSource].setID,
  212.                         theISTProperty[kISTChipInterruptSource].member,
  213.                         &theRefcon,
  214.                         &defaultISRFunction,
  215.                         &defaultEnablerFunction,
  216.                         &defaultDisablerFunction);
  217.         
  218. if (osStatus == kOTNoError)
  219.     {
  220.     osStatus = InstallInterruptFunctions(theISTProperty[kISTChipInterruptSource].setID, 
  221.                         theISTProperty[kISTChipInterruptSource].member, 
  222.                         NULL, 
  223.                         (InterruptHandler )ABCVendorISR, 
  224.                         NULL, 
  225.                         NULL);
  226.  
  227.     if ((osStatus == kOTNoError) && defaultEnablerFunction)
  228.         defaultEnablerFunction(theISTProperty[kISTChipInterruptSource],theRefcon);
  229.     }
  230.     
  231. return osStatus;
  232. }
  233.     
  234. //-----------------------------------------------------------------------------------------
  235. //    Description:
  236. //        This routine uninstalls the interrupt service routine for our card.
  237. //
  238. //    Input:
  239. //        NONE
  240. //
  241. //    Output:
  242. //        returns error if problem occurs during uninstall of isr
  243. //
  244. //-----------------------------------------------------------------------------------------
  245. OSStatus UninstallISR(void)
  246. {
  247. void                    *theRefcon;
  248. InterruptHandler        defaultISRFunction;        
  249. InterruptEnabler        defaultEnablerFunction;    
  250. InterruptDisabler        defaultDisablerFunction;
  251. OSStatus                osStatus;
  252. RegPropertyValueSize    propertySize;
  253. ISTProperty                theISTProperty;
  254.  
  255. propertySize = sizeof(ISTProperty);
  256.  
  257. osStatus = RegistryPropertyGet(&gPortPrivateData->nodeEntryID,
  258.                                 kISTPropertyName,
  259.                                 &theISTProperty,
  260.                                 &propertySize);
  261.                                 
  262. if (osStatus)
  263.     return osStatus;
  264.  
  265. osStatus = GetInterruptFunctions(theISTProperty[kISTChipInterruptSource].setID,
  266.                         theISTProperty[kISTChipInterruptSource].member,
  267.                         &theRefcon,
  268.                         &defaultISRFunction,
  269.                         &defaultEnablerFunction,
  270.                         &defaultDisablerFunction);
  271.         
  272. if (osStatus == kOTNoError)
  273.     {
  274.     osStatus = InstallInterruptFunctions(theISTProperty[kISTChipInterruptSource].setID, 
  275.                         theISTProperty[kISTChipInterruptSource].member, 
  276.                         theRefcon, 
  277.                         defaultISRFunction, 
  278.                         defaultEnablerFunction, 
  279.                         defaultDisablerFunction);
  280.  
  281.     if ((osStatus == kOTNoError) && defaultEnablerFunction)
  282.         defaultDisablerFunction(theISTProperty[kISTChipInterruptSource],theRefcon);
  283.     }
  284.     
  285. return osStatus;
  286. }
  287.  
  288.